package com.gopay.bis.web.controller.expay;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.YeePayConstants;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.bank.yeepay.expay.YeePayExPayMessage;

@Controller
@Scope("prototype")
public class YeePayCreditExPayServerRes extends ExPayBankResBase {
	private static Log logger = LogFactory.getLog(YeePayDebitExPayServerRes.class);

	@RequestMapping(value = "/yeePayCreditExPayServerRes")
	protected ModelAndView execute(HttpServletRequest request,
			HttpServletResponse response) {
		logger.error("--------------- YeePayCreditExPayServerRes bankback  ---------------");
		return super.execute(request, response);
	}

	@Override
	protected ExPayBaseMessage buildMessage(HttpServletRequest request) {
		YeePayExPayMessage message = new YeePayExPayMessage();
		message.setBank(BankConstants.BANK_LOGO_YEEPAY);
		message.setBankBusProd(BisBankBusinessProd._05);
		// 银行接口通道信息 09: 易宝快捷
        message.setBankIntrChann(BisBankInterfaceChannel._09);
		message.setData(request.getParameter("data"));
		message.setEncryptkey(request.getParameter("encryptkey"));
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		YeePayExPayMessage message = (YeePayExPayMessage) result
				.getModel("res");
		logger.error("YeePayCreditExPayServerRes notify:"
				+ ToStringBuilder.reflectionToString(message));
		OrderInfo orderInfo = new OrderInfo();
		if (message != null) {
			String status = message.getStatus(); // 订单状态
			String amt = TransUtils.amountConvert2Yuan(message.getAmount().toString());
			if (status != null
					&& YeePayConstants.YEEPAY_EXPAY_PAYMENT_SUCCESS
							.equals(status)) {
				orderInfo.setPayStatus("00");
				orderInfo.setRetCode("00");
				orderInfo.setRetMsg("");
			} else {
				orderInfo.setPayStatus("01");
				orderInfo.setRetCode("01");
				orderInfo.setRetMsg(message.getErrormsg());
			}
			orderInfo.setOrderId(message.getOrderId());// 为了获取系统订单号
			orderInfo.setAmt(amt);
			orderInfo.setNotifyNum("1");
		}
		orderInfo.setCardType("");// 信用卡类型
		orderInfo.setIsNotify(false);
		orderInfo.setBankName("yeepay");
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("data"))
				|| StringUtils.isEmpty(request.getParameter("encryptkey"))) {
			return false;
		}
		return true;
	}

}
